********************************************************************************
**	PURPOSE: Predict 18 month denlinquency index with ficoscore08 at 6 month endline - create appendix table 9
**							
**	INPUTS: Wide with outcomes.dta
**	
**	OUTPUTS: Preditive Power_index_e6.xls 
**
**	CREATED/EDITED BY: Kayla Wilding, Leah Kim
**
**	DATE CREATED: 7/2017
**
**	DATE LAST EDITED: 4/8/2022
********************************************************************************
clear all

use "$adta/Wide with outcomes.dta", clear

loc indep_year "12 6" 
loc suffix `" "_e12" "_e6" "' // suffix for independent variable
loc suffix_o `" "" "_f4" "_l6" "_1812" "f4_1812" "l6_1812" "' // suffix for dependent variable

local outcomesi z_default_index_all_i18 z_default_index_f4_i18 z_default_index_l6_i18 z_default_index_all_i18_12 z_default_index_f4_i18_12 z_default_index_l6_i18_12
local outcomesl bin30acctsdl18 bin90acctsdl18 num30acctsdl18 num90acctsdl18  numcollect18 totamtpdt18 numacctsderog18 ///
				bincollect18 bintotamtpd18 binacctsderog18 
	
		
local restriction flag_randomized
local errors robust
local decs = 3
local tfilenamei "Predictive Power_index"
local tfilenamea "Predictive Power_components"

local titlei "Table 9. Do CBLs Change the Gradient of Credit Scores?"
local titlea "Appendix Table 9. Do CBLs Change the Gradient of Credit Scores? Average Treatment Effects on Index Components"

*Create new variables

gen z_default_index_all_i18_12 = z_default_index_all_i18 - z_default_index_all_i12
gen z_default_index_f4_i18_12 = z_default_index_f4_i18 - z_default_index_f4_i12
gen z_default_index_l6_i18_12 = z_default_index_l6_i18 - z_default_index_l6_i12

gen num30acctsdl18_12 = num30acctsdl18 - num30acctsdl12
gen num90acctsdl18_12 = num90acctsdl18 - num90acctsdl12
gen bin30acctsdl18_12 = (num30acctsdl18_12 > 0) if ~missing(num30acctsdl18_12)
gen bin90acctsdl18_12 = (num90acctsdl18_12 > 0) if ~missing(num90acctsdl18_12) 

qui forval v = 1/`: word count `outcomesi'' {
	loc var `:word `v' of `outcomesi''
	loc o `:word `v' of `suffix_o''
	
	forval i=1/`: word count `indep_year'' {
	loc x `:word `i' of `indep_year''
	loc s `:word `i' of `suffix''
	
	reg `var'  ficoscore08h1 1.flag_cblgroup#c.ficoscore08h`x' 1.flag_extragroup#c.ficoscore08h`x', vce(`errors') // 
	local obs = e(N)
	sum `var' if e(sample) == 1 & flag_extragroup == 1 
	local mu_lhs = string(r(mean), "%14.3f")
	test 1.flag_cblgroup#c.ficoscore08h`x' = 1.flag_extragroup#c.ficoscore08h`x'
	local tstat1 = string(r(p), "%14.`decs'f")
	#delimit ;
	outreg2 using "$outputtables/`tfilenamei'`s'`o'.xls", replace noobs sortvar(1.flag_cblgroup#c.ficoscore08h`x' 1.flag_extragroup#c.ficoscore08h`x') label nocons dec(`decs') nor2 addtext(
	P-value for encficoh`x' = extraficoh`x', `tstat1', 
	P-value for encficolowh`x' = extraficolowh`x', "N/A",
	P-value for encficomedh`x' = extraficomedh`x', "N/A", 
	P-value for encficohighh`x' = extraficohighh`x', "N/A", 
	Obs, `obs', Mean Dependent Variable in Extra Step Group @ 18-month endline, `mu_lhs') nonote title("`titlei'") slow(8000) noaster ;
	#delimit cr ;
	sleep 4000
	
	reg `var'  ficoscore08h1 1.flag_cblgroup#c.ficoscore08h`x'#1.ltca ///
			1.flag_extragroup#c.ficoscore08h`x'#1.ltca ///
			1.flag_cblgroup#c.ficoscore08h`x'#1.mtca ///
			1.flag_extragroup#c.ficoscore08h`x'#1.mtca ///
			1.flag_cblgroup#c.ficoscore08h`x'#1.htca ///
			1.flag_extragroup#c.ficoscore08h`x'#1.htca , vce(`errors') 
	local obs = e(N)
	sum `var' if e(sample) == 1 & flag_extragroup == 1 
	test 1.flag_cblgroup#c.ficoscore08h`x'#1.ltca = 1.flag_extragroup#c.ficoscore08h`x'#1.ltca 
	local tstat2 = string(r(p), "%14.`decs'f")
	test 1.flag_cblgroup#c.ficoscore08h`x'#1.mtca = 1.flag_extragroup#c.ficoscore08h`x'#1.mtca 
	local tstat3 = string(r(p), "%14.`decs'f")
	test 1.flag_cblgroup#c.ficoscore08h`x'#1.htca = 1.flag_extragroup#c.ficoscore08h`x'#1.htca
	local tstat4 = string(r(p), "%14.`decs'f")
	#delimit ;
	outreg2 using "$outputtables/`tfilenamei'`s'`o'.xls", append noobs sortvar(`=cond("`x'"=="_121","","ficoscore08h1")'
			1.flag_cblgroup#c.ficoscore08h`x'
			1.flag_extragroup#c.ficoscore08h`x'
			1.flag_cblgroup#c.ficoscore08h`x'#1.ltca
			1.flag_extragroup#c.ficoscore08h`x'#1.ltca 
			1.flag_cblgroup#c.ficoscore08h`x'#1.mtca 
			1.flag_extragroup#c.ficoscore08h`x'#1.mtca 
			1.flag_cblgroup#c.ficoscore08h`x'#1.htca 
			1.flag_extragroup#c.ficoscore08h`x'#1.htca)  label nocons dec(`decs') nor2 addtext(
	P-value for encficoh`x' = extraficoh`x', "N/A", 
	P-value for encficolowh`x' = extraficolowh`x', `tstat2',
	P-value for encficomedh`x' = extraficomedh`x', `tstat3', 
	P-value for encficohighh`x' = extraficohighh`x', `tstat4', 
	Obs, `obs', Mean Dependent Variable in Extra Step Group @ 18-month endline, `mu_lhs') nonote title("`titlei'") slow(8000) noaster; 
	#delimit cr ;
	sleep 4000
	
	}
	
}

qui forval t=1/`: word count `indep_year'' {
	local loopcount 1
	loc x `:word `t' of `indep_year''
	loc s `:word `t' of `suffix''
	
	forval v=1/`: word count `outcomesl''{
		loc var `:word `v' of `outcomesl''

		if `loopcount' == 1 {
			local action "replace"
		}
		else {
			local action "append"
		}

		reg `var'  1.flag_cblgroup#c.ficoscore08h`x' 1.flag_extragroup#c.ficoscore08h`x', vce(`errors') 
		local obs = e(N)
		sum `var' if e(sample) == 1 & flag_extragroup == 1 
		local mu_lhs = string(r(mean), "%14.3f")
		test 1.flag_cblgroup#c.ficoscore08h`x' = 1.flag_extragroup#c.ficoscore08h`x'
		local tstat1 = string(r(p), "%14.`decs'f")
		#delimit ;
		outreg2 using "$outputtables/`tfilenamea'`s'.xls", `action' noobs sortvar(1.flag_cblgroup#c.ficoscore08h`x' 1.flag_extragroup#c.ficoscore08h`x') label nocons dec(`decs') nor2 addtext(
		P-value for encficoh`x' = extraficoh`x', `tstat1', 
		P-value for encficolowh`x' = extraficolowh`x', "N/A",
		P-value for encficomedh`x' = extraficomedh`x', "N/A", 
		P-value for encficohighh`x' = extraficohighh`x', "N/A", 
		Obs, `obs', Mean Dependent Variable in Extra Step Group @ `x'-month endline, `mu_lhs') nonote title("`titlei'") slow(8000) noaster ;
		#delimit cr ;
		sleep 4000

		reg `var'	 1.flag_cblgroup#c.ficoscore08h`x'#1.ltca ///
			1.flag_extragroup#c.ficoscore08h`x'#1.ltca ///
			1.flag_cblgroup#c.ficoscore08h`x'#1.mtca ///
			1.flag_extragroup#c.ficoscore08h`x'#1.mtca ///
			1.flag_cblgroup#c.ficoscore08h`x'#1.htca ///
			1.flag_extragroup#c.ficoscore08h`x'#1.htca , vce(`errors') 
		local obs = e(N)
		sum `var' if e(sample) == 1 & flag_extragroup == 1 
		test 1.flag_cblgroup#c.ficoscore08h`x'#1.ltca = 1.flag_extragroup#c.ficoscore08h`x'#1.ltca 
		local tstat2 = string(r(p), "%14.`decs'f")
		test 1.flag_cblgroup#c.ficoscore08h`x'#1.mtca = 1.flag_extragroup#c.ficoscore08h`x'#1.mtca 
		local tstat3 = string(r(p), "%14.`decs'f")
		test 1.flag_cblgroup#c.ficoscore08h`x'#1.htca = 1.flag_extragroup#c.ficoscore08h`x'#1.htca
		local tstat4 = string(r(p), "%14.`decs'f")
		#delimit ;
		outreg2 using "$outputtables/`tfilenamea'`s'.xls", append noobs sortvar(ficoscore08h1
				1.flag_cblgroup#c.ficoscore08h`x' 
				1.flag_extragroup#c.ficoscore08h`x' 
				1.flag_cblgroup#c.ficoscore08h`x'#1.ltca
				1.flag_extragroup#c.ficoscore08h`x'#1.ltca 
				1.flag_cblgroup#c.ficoscore08h`x'#1.mtca 
				1.flag_extragroup#c.ficoscore08h`x'#1.mtca 
				1.flag_cblgroup#c.ficoscore08h`x'#1.htca 
				1.flag_extragroup#c.ficoscore08h`x'#1.htca)  label nocons dec(`decs') nor2 addtext(
		P-value for encficoh`x' = extraficoh`x', "N/A", 
		P-value for encficolowh`x' = extraficolowh`x', `tstat2',
		P-value for encficomedh`x' = extraficomedh`x', `tstat3', 
		P-value for encficohighh`x' = extraficohighh`x', `tstat4', 
		Obs, `obs', Mean Dependent Variable in Extra Step Group @ `x'-month endline, `mu_lhs') nonote title("`titlei'") slow(8000) noaster; 
		#delimit cr ;
		sleep 4000
		local ++loopcount
	}
}


****EOF***

